package com.zeratul.common.i18n.config;

import com.zeratul.common.i18n.web.mvc.WebMvcLocaleResolver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.LocaleResolver;


/**
 * 基于Servlet模式的国际化配置
 *
 * 设置自动装配的顺序，该装配类 必须比 WebMvcAutoConfiguration 先加载
 * 因为WebMvcAutoConfiguration里配置了国际化处理器Bean LocaleResolver
 * 会导致自己配置的LocaleResolver Bean对象不起作用
 */
@AutoConfiguration(before = WebMvcAutoConfiguration.class)
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@ConditionalOnProperty(prefix = "i18n.config", name = "enabled", havingValue = "true", matchIfMissing = true)
@Slf4j
public class I18nWebMvcAutoConfiguration {

    /**
     * 根据请求头Accept-Language切换语言环境
     * @return
     */
    @Bean
    public LocaleResolver localeResolver() {
        log.debug("【国际化】Mvc模式-国际化语言环境加载");
        return new WebMvcLocaleResolver();
    }
}
